package com.md24housecall.fibromyalgiadiagnostic;


import com.md24housecall.fibromyalgiadiagnostic.LoadingTask.LoadingTaskFinishedListener;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ResultFragment extends Fragment implements LoadingTaskFinishedListener{
    final static String ARG_POSITION = "position";
    int mCurrentPosition = -1;
    int questionThreeScoreTotal = 0;
    @SuppressLint("ResourceAsColor")
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous article selection set by onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.
        if (savedInstanceState != null) {
            mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
        }

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.result_fragment, container, false);
        
        
        Button button = (Button) view.findViewById(R.id.next_button);
        button.setOnClickListener((OnClickListener) new ButtonEvent());
        
        
        int questionThreeScoreTotal = ((MainActivity)getActivity()).getQuestionThreeScore();
        int questionOneScoreTotal = ((MainActivity)getActivity()).getQuestionOneScore();
        int questionTwoScoreTotal = ((MainActivity)getActivity()).getQuestionTwoScore();
        
       
        
        int questionThreeScore;
        if(questionThreeScoreTotal == 0)
    		questionThreeScore = 0;
    	else if(questionThreeScoreTotal <= 10)
    		questionThreeScore = 1;
    	else if (questionThreeScoreTotal <= 24)
    		questionThreeScore = 2;
    	else
    		questionThreeScore = 3;
        
        int wpiScore = questionOneScoreTotal;
        int ssScore = questionTwoScoreTotal + questionThreeScore;
        
        TextView wpiScoreView = (TextView) view.findViewById(R.id.wpi_score);
        wpiScoreView.setText("Your WPI score is: " + wpiScore);
        
        TextView ssScoreView = (TextView) view.findViewById(R.id.ss_score);
        ssScoreView.setText("Your SS score is: " + ssScore);
       
        int wouldMeet;
		if( (wpiScore >= 7 && ssScore >= 5) ||
        		(wpiScore >= 3 && wpiScore <= 6 && ssScore >= 9) )
        	wouldMeet = 1;
        else
        	wouldMeet = 0;
        
		//upload result to server
		String email = getActivity().getIntent().getStringExtra("email");
        String params = "update_score/email/"+email+"/ss_score/"+ssScore+"/wpi_score/"+wpiScore+"/would_meet/"+wouldMeet;
        
        new LoadingTask(this, this.getActivity()).execute(params); 
	        
		TextView resultView = (TextView) view.findViewById(R.id.result_text);
		LinearLayout callLayout = (LinearLayout) view.findViewById(R.id.call_layout);
		
		if(wouldMeet == 1){
			resultView.setText("You would meet the new fibromyalgia diagnostic criteria.");
			resultView.setTextColor(view.getResources().getColor(R.color.warning));
			
			TextView conditionsView = (TextView) view.findViewById(R.id.conditions);
			conditionsView.setText(view.getResources().getString(R.string.conditions));
			callLayout.setVisibility(View.INVISIBLE);
		}else{
			resultView.setText("Sorry, you would not meet the new fibromyalgia diagnostic criteria.");
			resultView.setTextColor(view.getResources().getColor(R.color.not_warning));
			
			Button callUsButton = (Button) view.findViewById(R.id.call_us);
	        callUsButton.setOnClickListener((OnClickListener) new CallUs());
	        callLayout.setVisibility(View.VISIBLE);
		}
		return view;
    }

    private class CallUs implements OnClickListener{

        @Override
        public void onClick(View v) {
        	Intent browserIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:8886324758"));
        	startActivity(browserIntent);

        }

    }
    
    private class ButtonEvent implements OnClickListener{

        @Override
        public void onClick(View v) {
        	TroubleshootingFragment newFragment = new TroubleshootingFragment();
            Bundle args = new Bundle();
            //args.putInt(NoticeFragment.ARG_POSITION, position);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.setCustomAnimations(R.anim.in_from_right, R.anim.out_to_left, R.anim.in_from_left, R.anim.out_to_right);

            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null).commit();
        }

    }
    
    @Override
    public void onStart() {
        super.onStart();

        // During startup, check if there are arguments passed to the fragment.
        // onStart is a good place to do this because the layout has already been
        // applied to the fragment at this point so we can safely call the method
        // below that sets the article text.
        Bundle args = getArguments();
        if (args != null) {
            // Set article based on argument passed in
           // updateArticleView(args.getInt(ARG_POSITION));
        } else if (mCurrentPosition != -1) {
            // Set article based on saved instance state defined during onCreateView
            //updateArticleView(mCurrentPosition);
        }
    }

   
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current article selection in case we need to recreate the fragment
        outState.putInt(ARG_POSITION, mCurrentPosition);
    }


	@Override
	public void onTaskFinished() {
		// TODO Auto-generated method stub
		
	}
}